/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.example;

import com.facebook.rebound.SpringSystem;
import com.tumblr.backboard.Actor;
import com.tumblr.backboard.MotionProperty;
import com.tumblr.backboard.Utils.View;
import com.tumblr.backboard.imitator.MotionImitator;
import ohos.agp.components.Component;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Snap a view to either the lower left or lower right corner.
 *
 * @author ericleong
 * @since 2021-05-07
 */
public class SnapFragment extends BaseFraction {
    @Override
    public int getUiContent() {
        return ResourceTable.Layout_fragment_snap;
    }

    @Override
    public void initComponent() {
        Component circle = mComponent.findComponentById(ResourceTable.Id_circle);
        new Actor.Builder(SpringSystem.create(), circle).addTranslateMotion(MotionProperty.Y)
                .addMotion(new MotionImitator(MotionProperty.X) {
                    @Override
                    public void release(TouchEvent event) {
                        // snap to left or right depending on current location
                        if (mSpring.getCurrentValue() > mComponent.getWidth() / VALUE_TWO
                                - circle.getWidth() / VALUE_TWO) {
                            mSpring.setEndValue(mComponent.getWidth() - circle.getWidth());
                        } else {
                            mSpring.setEndValue(0);
                        }
                    }
                }, View.TRANSLATION_X).build();
    }
}